<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="Hcur(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEsit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="resetForm.page"
                :page-sizes="[2, 4, 8, 16]"
                :page-size="resetForm.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹层组件 -->
      <el-dialog
        :title="isShow?'编辑':'新增'"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="50%"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="dialogVisible = false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>

      </el-dialog>

      <!-- 分配权限 -->
      <el-dialog
        title="分配权限"
        :visible.sync="showDialogAssign"
      >
        <AssignPermission v-if="showDialogAssign" :cur-id="curId" @succer="showDialogAssign = false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRole, addRole, putRole, deleteRole } from '@/api/settings'
import AssignPermission from './assignPermission.vue'
export default {
  name: 'VueWhZr3Settings',

  components: {
    AssignPermission
  },

  data() {
    return {
      list: [],
      showDialogAssign: false,
      curId: '',
      resetForm: {
        page: 1, // 页码值
        pagesize: 2 // 每页显示几条数据
      },
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请选择角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请选择角色描述', trigger: 'blur' }
        ]
      },
      total: 0,
      dialogVisible: false, // 弹层
      isShow: false // false新增 || true编辑

    }
  },

  created() {
    this.lodeRole()
  },

  methods: {
    Hcur(id) {
      this.showDialogAssign = true
      this.curId = id
    },
    // 清空表单和校验规则
    hClose() {
      // 清空校验规则
      this.$refs.roleForm.resetFields()
      // 清空表单
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 校验
    hSubmit() {
      // 兜底校验
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return // 校验失败
        this.isShow ? this.doEsit() : this.lodeaddRole()
      })
    },
    hEsit(row) {
      // 编辑的状态
      // console.log(row)
      this.roleForm = { ...row } // 回显功能
      this.dialogVisible = true
      this.isShow = true
    },
    hAdd() {
      // 新增的状态
      this.dialogVisible = true
      this.isShow = false
    },
    // 删除
    hDel(id) {
      this.$confirm('你确定要删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteRole(id)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        if (this.list.length === 1) {
          this.resetForm.page--
        }
        if (this.resetForm.page <= 0) {
          this.resetForm.page = 1
        }
        // 删除成功重新获取角色列表，渲染
        this.lodeRole()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑
    async  doEsit() {
      try {
        const res = await putRole(this.roleForm)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.dialogVisible = false
        this.lodeRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 新增
    async lodeaddRole() {
      try {
        const res = await addRole(this.roleForm)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.dialogVisible = false
        this.resetForm.page++
        const max = Math.ceil(this.total / this.resetForm.page)
        this.resetForm.page = max
        this.lodeRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取角色列表
    async lodeRole() {
      try {
        const res = await getRole(this.resetForm)
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.total = res.data.total
        this.list = res.data.rows
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.resetForm.pagesize = val
      this.resetForm.page = 1
      this.lodeRole()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.resetForm.page = val
      this.lodeRole()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
